package com.chao.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chao.entity.SysMsg;
import com.chao.entity.SysNotice;
import com.chao.service.impl.SysMsgServiceImpl;
import com.chao.service.impl.SysNoticeServiceImpl;
import com.chao.utils.TypeConversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;
import java.util.List;

@Controller
public class SystemController {

    @Autowired
    SysNoticeServiceImpl sysNoticeService;
    @Autowired
    SysMsgServiceImpl msgService;

    // 删除留言
    @GetMapping("/sysMsgDelete/{mIds}")
    public String sysMsg_delete(@PathVariable String mIds,
                                @RequestParam Integer pn,
                                RedirectAttributes ra) {
        if (mIds.contains("-")) {
            String[] strings = mIds.split("-");
            List<Integer> integers = TypeConversion.strArrToIntegerList(strings);
            msgService.removeByIds(integers);
        } else {
            msgService.removeById(Integer.parseInt(mIds));
        }
        ra.addAttribute("pn", pn);
        return "redirect:/sys_msg";
    }

    // 回复
    @PutMapping("/sysMsgReply")
    public String sysMsg_reply(SysMsg sysMsg,
                               @RequestParam Integer pn,
                               RedirectAttributes ra) {
        sysMsg.setMStatus(1);
        msgService.updateById(sysMsg);
        ra.addAttribute("pn", pn);
        return "redirect:/sys_msg";
    }

    // 添加留言
    @PostMapping("/sysMsgAdd")
    public String sysMsg_add(SysMsg sysMsg,
                             @RequestParam Integer pn,
                             RedirectAttributes ra) {
        sysMsg.setMDate(new Date());
        sysMsg.setMStatus(0);
        msgService.addOfPrimary(sysMsg);
        ra.addAttribute("pn", pn);
        return "redirect:/sys_msg";
    }

    // 系统留言界面
    @GetMapping("/sys_msg")
    public String sys_msg(@RequestParam(defaultValue = "1") Integer pn,
                          Model model) {
        Page<SysMsg> page = new Page<>(pn, 5);
        Page<SysMsg> sysMsgPage = msgService.page(page);
        List<SysMsg> sysMsgs = sysMsgPage.getRecords();
        model.addAttribute("page", page);
        model.addAttribute("sysMsgs", sysMsgs);
        return "msg/sys_msg";
    }

    /****
     *
     *
     *
     */

    // 系统公告页面
    @GetMapping("/sys_notice")
    public String sys_notice(@RequestParam(defaultValue = "1") Integer pn,
                             Model model) {
        Page<SysNotice> page = new Page<>(pn, 5);
        Page<SysNotice> sysNoticePage = sysNoticeService.page(page);
        List<SysNotice> sysNotices = sysNoticePage.getRecords();
        model.addAttribute("page", page);
        model.addAttribute("sysNotices", sysNotices);
        return "msg/sys_notice";
    }

    // 添加系统公告
    @PostMapping("/sysNoticeAdd")
    public String sys_notice_add(SysNotice sysNotice,
                                 @RequestParam Integer pn,
                                 RedirectAttributes ra) {
        sysNotice.setNDate(new Date());
        sysNoticeService.addOfPrimary(sysNotice);
        ra.addAttribute("pn", pn);
        return "redirect:/sys_notice";
    }

    // 修改公告
    @PutMapping("/sysNoticeUpdate")
    public String sys_notice_update(SysNotice sysNotice,
                                    @RequestParam Integer pn,
                                    RedirectAttributes ra) {
        sysNotice.setNDate(new Date());
        sysNoticeService.updateById(sysNotice);
        ra.addAttribute("pn", pn);
        return "redirect:/sys_notice";
    }

    // 删除公告
    @DeleteMapping("/sysNoticeDelete")
    public String sys_notice_update(@RequestParam String nIds,
                                    @RequestParam Integer pn,
                                    RedirectAttributes ra) {
        if (nIds.contains("-")) {
            String[] strings = nIds.split("-");
            List<Integer> integers = TypeConversion.strArrToIntegerList(strings);
            sysNoticeService.removeByIds(integers);
        } else {
            sysNoticeService.removeById(Integer.parseInt(nIds));
        }
        ra.addAttribute("pn", pn);
        return "redirect:/sys_notice";
    }

}
